package xyz.fcidd.monitor.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import xyz.fcidd.monitor.dto.UserDTO;
import xyz.fcidd.monitor.dto.UserJoinDTO;
import xyz.fcidd.monitor.mapper.UserMapper;
import xyz.fcidd.monitor.service.UserService;
import xyz.fcidd.monitor.session.MybatisSession;

import java.time.LocalDateTime;

public class UserServiceImpl implements UserService {
    @Override
    public void joinPlayer(PlayerJoinEvent joinEvent) {
        //获取加入玩家的信息
        Player player = joinEvent.getPlayer();
        //获取玩家的名字
        String playerName = player.getName();
        //获取玩家的IP
        String playerIP = player.getAddress().toString();
        String ip = playerIP.substring(1, playerIP.lastIndexOf(":"));
        //获取玩家的UUID
        String playerUUID = player.getUniqueId().toString();
        //获取玩家的等级
        int playerLevel = player.getLevel();
        //获取玩家是否是OP
        int playerOp = player.isOp() ? 1 : 0;
        //获取玩家是否被Ban
        int playerBan = player.isBanned() ? 1 : 0;
        //获取sqlServer的session
        SqlSession sqlSession = MybatisSession.getSqlSession();
        //获取UserMapper此类的方法
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        UserJoinDTO joinDTO = new UserJoinDTO();
        joinDTO.setUsername(playerName);
        //查询数据库是否存在此用户
        UserJoinDTO userName = userMapper.selectUserName(joinDTO);
        UserDTO userDTO = new UserDTO();
        //如果为空
        if (userName == null) {
            //将用户数据写入数据库里
            userDTO.setUsername(playerName);
            userDTO.setIp(ip);
            userDTO.setUid(playerUUID);
            userDTO.setLevel(playerLevel);
            userDTO.setOp(playerOp);
            userDTO.setBan(playerBan);
            userDTO.setLoginTime(LocalDateTime.now());
            userMapper.insert(userDTO);
            //将session提交给数据库并关闭
            sqlSession.commit();
            sqlSession.close();
        }
        if (userName != null) {
            //从数据库里获取登录时间
            LocalDateTime loginTime = userMapper.selectLoginTime(joinDTO);
            //更新用户数据
            userDTO.setUsername(playerName);
            userDTO.setIp(ip);
            userDTO.setUid(playerUUID);
            userDTO.setLevel(playerLevel);
            userDTO.setOp(playerOp);
            userDTO.setBan(playerBan);
            userDTO.setLastTime(loginTime);
            userDTO.setLoginTime(LocalDateTime.now());
            userMapper.updateLoginDate(userDTO);
            //将session提交给数据库并关闭
            sqlSession.commit();
            sqlSession.close();
        }
    }

    @Override
    public void quitPlayer(PlayerQuitEvent quitEvent) {
        //获取用户数据
        Player player = quitEvent.getPlayer();
        String playerName = player.getName();
        int playerLevel = player.getLevel();
        int playerOp = player.isOp() ? 1 : 0;
        int playerBan = player.isBanned() ? 1 : 0;
        //获取sqlserver的session
        SqlSession sqlSession = MybatisSession.getSqlSession();
        //获取userMapper此类的方法
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //将玩家离开的数据更新到数据库
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(playerName);
        userDTO.setLevel(playerLevel);
        userDTO.setOp(playerOp);
        userDTO.setBan(playerBan);
        userDTO.setOutTime(LocalDateTime.now());
        mapper.updateOutDate(userDTO);
        sqlSession.commit();
        sqlSession.close();
    }

}
